package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.R;
import com.itheima.common.pageParm;
import com.itheima.dao.employeeDao;
import com.itheima.service.employeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

@Transactional
@Service
public class employeeServiceImpl implements employeeService {

    @Resource
    private employeeDao employeeDao;

    @Override
    public Employee login(Employee employee) {


        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        lqw.eq(Employee::getUsername, employee.getUsername());
        //
        //String pwd= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //lqw.eq(Employee::getPassword,pwd);

        lqw.eq(Employee::getPassword, employee.getPassword());


        return employeeDao.selectOne(lqw);

    }

    @Override
    public int insert(Employee employee) {

        return employeeDao.insert(employee);
    }

    @Override
    public IPage<Employee> findPage(pageParm pageParm) {

        //构建Ipage对象 将参数传递进去

        IPage<Employee> p = new Page<>(pageParm.getPage(), pageParm.getPageSize());


        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        lqw.like(pageParm.getName() != null, Employee::getName, pageParm.getName());

        return employeeDao.selectPage(p, lqw);
    }

    @Override
    public int update(Employee employee) {


        return employeeDao.updateById(employee);


    }
}
